<template>
  <!-- 友链 -->
  <div class="friend">
    <h3>共此烟霞为故旧，何须执手话离殇</h3>
    <div class="apply">
      <el-collapse accordion>
        <el-collapse-item name="1">
          <template #title>
            <div class="title">友链申请</div>
          </template>
          <el-form
            :inline="true"
            :model="applyMaterial"
            :rules="rules"
            ref="applyForm"
          >
            <el-form-item label="昵称" prop="name">
              <el-input
                placeholder="请填写昵称"
                v-model="applyMaterial.name"
              ></el-input>
            </el-form-item>
            <el-form-item label="联系邮箱" prop="email">
              <el-input
                placeholder="请填写联系邮箱"
                v-model="applyMaterial.email"
              ></el-input>
            </el-form-item>
            <el-form-item label="博客链接" prop="blogUrl">
              <el-input
                placeholder="请填写博客链接"
                v-model="applyMaterial.blogUrl"
              ></el-input>
            </el-form-item>
            <el-form-item label="请简短的描述一下您的blog" prop="desc">
              <el-input
                placeholder="例如这是一个....的博客"
                v-model="applyMaterial.desc"
              ></el-input>
            </el-form-item>
            <el-form-item label="请选择展示头像上传">
              <el-upload
                v-model:file-list="successImgArr"
                :action="serverUploadImg"
                list-type="picture"
                :on-success="handleSuccess"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :on-error="handleError"
                :on-exceed="handleExceed"
                :limit="1"
              >
                <el-button
                  size="small"
                  style="background: #cae6f7; border: none; color: #fff"
                  >选择头像</el-button
                >
              </el-upload>
            </el-form-item>
            <el-form-item>
              <Button
                @click="submitApply"
                label="提交申请"
                severity="info"
                rounded
                size="small"
              ></Button>
            </el-form-item>
          </el-form>
        </el-collapse-item>
      </el-collapse>
    </div>

    <ProgressSpinner
      v-if="progress"
      style="width: 50px; height: 50px; display: flex; justify-content: center"
      strokeWidth="8"
      fill="transparent"
      animationDuration=".5s"
      aria-label="Custom ProgressSpinner"
    />

    <div class="body">
      <Friend
        v-for="item in friendList"
        :key="item.id"
        :info="item"
        @click="goUrl(item.blogUrl)"
      />
    </div>
    <div class="page">
      <el-pagination
        v-model:current-page="query.page"
        :page-size="query.pageSize"
        :disabled="false"
        layout="total, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, useTemplateRef } from "vue";
import Friend from "./components/Friend.vue";
import { serverStore } from "@/stores/server";
import { ElMessage } from "element-plus";
import { applyFriendService, getFriendListService } from "@/api/Friend";

const { serverUploadImg } = serverStore();

const progress = ref(true);

const friendList = ref([]);

/**
 * 表单数据
 */
const applyMaterial = ref({
  name: "",
  email: "",
  headerUrl: "",
  blogUrl: "",
  desc: "",
});
const form = useTemplateRef("applyForm");
// 表单校验
const rules = {
  name: [
    {
      required: true,
      message: "昵称不能为空",
      trigger: "blur",
    },
  ],
  email: [
    {
      required: true,
      message: "邮箱不能为空",
      trigger: "blur",
    },
  ],
  blogUrl: [
    {
      required: true,
      message: "博客链接不能为空",
      trigger: "blur",
    },
  ],
  desc: [
    {
      required: true,
      message: "描述信息不能为空哦,请你努力想一想",
      trigger: "blur",
    },
  ],
};

/**
 * 分页参数
 */
const total = ref(0);
const query = ref({
  page: 1,
  pageSize: 15,
});

const handleSizeChange = (pageSize) => {};
const handleCurrentChange = (page) => {};

// 上传头像的回调数据
const successImgArr = ref([]);
// 上传成功的回调函数
const handleSuccess = () => {
  ElMessage.success("上传成功");
  applyMaterial.value.headerUrl = successImgArr.value[0].response.msg;
  console.log(applyMaterial.value);
};
// 点击上传列表图片
const handlePreview = (file) => {
  console.log("列表", file);
};
// 移除出列表图片
const handleRemove = () => {
  ElMessage.success("移除图片");
  console.log("移除图片");
};
const handleError = () => {
  ElMessage.error("图片上传失败");
};
const handleExceed = () => {
  ElMessage.warning("超出上传图片上限");
};

/**
 * 提交申请
 */
const submitApply = () => {
  if (applyMaterial.value.headerUrl === "") {
    ElMessage.error("请选择头像上传");
    return false;
  }
  form.value.validate((valid) => {
    if (valid) {
      // 通过表单校验
      console.log(applyMaterial.value);
      applyFriendService(applyMaterial.value).then(() => {
        applyMaterial.value = {
          name: "",
          email: "",
          headerUrl: "",
          blogUrl: "",
        };
        successImgArr.value = [];
      });
    }
  });
};

/**
 * 获取友链列表
 * @param pageQuery
 */
const getFriendList = async (pageQuery) => {
  await getFriendListService(pageQuery).then(({ data }) => {
    total.value = data.total;
    friendList.value = data.records;
    progress.value = false;
  });
};

const goUrl = (url) => {
  window.open(url, "_bank");
};

onMounted(() => {
  getFriendList(query.value);
});
</script>

<style lang="scss" scoped>
.friend {
  height: 102.5vh;
  padding: 24px 18px;
  background: #fff;
  border-radius: 0px 0px 12px 12px;
  box-shadow: 0 2px 7px rgba(0, 0, 0, 0.08);
  overflow: auto;
  scrollbar-width: none;

  h3 {
    margin-bottom: 50px;
  }

  .apply {
    min-height: 100px;
    margin: 36px 0px;

    .title {
      height: 50px;
      line-height: 50px;
      font-size: 15px;
      color: #6e6b6b;
      font-family: "Courier New", Courier, monospace;
    }
  }

  .body {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;

    @media (max-width: 768px) {
      padding: 0px 10px;
    }
  }

  .page {
    margin-top: 15px;
    display: flex;
    justify-content: end;
  }
}
</style>